<?php

namespace Crc\Entity;

use Doctrine\ORM\Mapping AS ORM,
    Doctrine\Common\Annotations\AnnotationRegistry,
    Crc\Annotations\Filters as Filter,
    Crc\Annotations\Validators as Assert,
    Crc\Annotations\Form\Elements as Element,
    Crc\Annotations\Grid as Grid;

/**
 *
 *
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @Grid\QuickActions(actions={"active", "inactive"})
 */

class User extends AbstractEntity
{
    /**
     * Default statuses
     */
    const STATUS_DELETED  = -1;
    const STATUS_INACTIVE =  0;
    const STATUS_ACTIVE   =  1;


    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Grid\Column(label="ID", show=true, default=false, sortable=true, order="ASC")
     * @Grid\Filter(name="number", label="ID")
     * @var int
     */
    protected $id;

    /**
     * @Filter\StringTrim
     * @Assert\EmailAddress
     * @ORM\Column(type="string", unique=true)
     * @Grid\Column(label="E-mail", show=true, default=true, sortable=true)
     * @Grid\Filter(name="text", label="E-mail")
     */
    protected $email;

    /**
     * @ORM\Column(type="string")
     * @Grid\Column(label="Name", show=true, default=true, sortable=true)
     * @Grid\Filter(name="text", label="Name")
     */
    protected $name;

    /**
     * @Element\Password
     * @ORM\Column(type="string", length=32)
     * @Grid\Column(show=false)
     */
    protected $pass;

    /**
     * @Filter\StringTrim
     * @Assert\StringLength(min=0, max=3)
     * @ORM\Column(type="string", length=3)
     * @Grid\Column(show=false)
     */
    protected $salt;

    /**
     * @Element\Textarea
     * @ORM\Column(type="string")
     * @Grid\Column(label="Address", show=true, default=false, sortable=true)
     * @Grid\Filter(name="text", label="Address")
     */
    protected $address;

    /**
     * @ORM\ManyToOne(targetEntity="AclRole", inversedBy="users")
     * @ORM\JoinColumn(name="id_acl_role", referencedColumnName="id")
     * @Grid\Column(label="Role", show=true, default=true, sortable=true)
     * @Grid\Filter(name="entity", label="Role", entity="AclRole")
     */
    protected $role;

    /**
     * @ORM\Column(type="string", length=32, nullable=true)
     * @Grid\Column(show=false)
     */
    protected $token;

    /**
     * @ORM\Column(type="integer")
     * @Grid\Column(label="Status", show=true, default=true, sortable=true)
     * @Grid\Filter(name="text", label="Status")
     * filter="array"
     */
    protected $status = 0;

    public function setPass($_pass)
    {
        $_pass = (strlen($_pass) == 32) ? $_pass : md5($_pass);
        $this->pass = $_pass;
        return $this;
    }

    public function processPassForAuth($_pass)
    {
        $_pass = md5($_pass);
        $_pass .= $this->getSalt();
        $_pass = md5($_pass);
        return $_pass;
    }

    public function setRole(AclRole $_role)
    {
        $_role->addUser($this);
        $this->role = $_role;
        return $this;
    }
}